//
//  NewsController.swift
//  FengHuangNews
//
//  Created by 新宝 on 2018/12/18.
//  Copyright © 2018年 LaiShengCai. All rights reserved.
//

import UIKit

protocol NewsViewDelegate: class{
    func newsViewDidScroll(_ controller:UIViewController,currentIndex:Int,scrollOffsetX:CGFloat)
}

class NewsController: UIViewController {
    weak var newsViewDelegate:NewsViewDelegate?
    var mainScrollView:UIScrollView = UIScrollView()
    let titleArray = ["体育新闻","头条","财经","娱乐圈","军事","历史","科技"]
    
    let sportVC = SportViewController()
    let headlinesVC = HeadlinesViewController()
    let ecnomicsVC = EconomicsViewController()
    let recreationVC = RecreationViewController()
    let militaryVC = MilitaryViewController()
    let historyVC = HistoryViewController()
    let technologVC = TechnologyViewController()
    
    @objc dynamic var name:String? //dynamic 修饰属性 表示该属性的存取都由 runtime 在运行时来决定
    
    //文字滑动栏
    lazy var headTitleView:HeadTitleView={
        let headTitleView=HeadTitleView()
        headTitleView.headTitleDelegate=self
        self.newsViewDelegate=headTitleView
        return headTitleView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        
        self.addObserver(self, forKeyPath: "name", options: .new, context: nil)
    }
    
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        let key = NSKeyValueChangeKey.newKey
        for dic in change! {
            if(dic.key==key){
                print("观察者--------%@",dic.value)
            }
        }
        
    }
    
    deinit {
        removeObserver(self, forKeyPath: "name")
        removeObserver(sportVC, forKeyPath: "name")
        removeObserver(headlinesVC, forKeyPath: "name")
        removeObserver(ecnomicsVC, forKeyPath: "name")
        removeObserver(recreationVC, forKeyPath: "name")
        removeObserver(militaryVC, forKeyPath: "name")
        removeObserver(historyVC, forKeyPath: "name")
        removeObserver(technologVC, forKeyPath: "name")
        print("---------挂了")
       
    }
    
   private func setupUI()  {
    mainScrollView = UIScrollView(frame: CGRect(x: 0, y: 60, width: ScreenWidth, height: ScreenHeight-60))
    let count:CGFloat = CGFloat(self.titleArray.count)
    mainScrollView.contentSize=CGSize(width: ScreenWidth*count, height: ScreenHeight-60)
    mainScrollView.showsHorizontalScrollIndicator=false
    mainScrollView.delegate=self as UIScrollViewDelegate
    mainScrollView.isPagingEnabled=true
    mainScrollView.bounces=false
    
    self.view.addSubview(headTitleView)
    self.view.addSubview(mainScrollView)

    //观察者
    self.addObserver(sportVC, forKeyPath: "name", options: .new, context: nil)
    self.addObserver(headlinesVC, forKeyPath: "name", options: .new, context: nil)
    self.addObserver(ecnomicsVC, forKeyPath: "name", options: .new, context: nil)
    self.addObserver(recreationVC, forKeyPath: "name", options: .new, context: nil)
     self.addObserver(militaryVC, forKeyPath: "name", options: .new, context: nil)
    self.addObserver(historyVC, forKeyPath: "name", options: .new, context: nil)
    self.addObserver(technologVC, forKeyPath: "name", options: .new, context: nil)
    

    
    sportVC.view.frame=CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight)
    headlinesVC.view.frame=CGRect(x: ScreenWidth, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    ecnomicsVC.view.frame=CGRect(x: ScreenWidth*2, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    recreationVC.view.frame=CGRect(x: ScreenWidth*3, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    militaryVC.view.frame=CGRect(x: ScreenWidth*4, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    historyVC.view.frame=CGRect(x: ScreenWidth*5, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    technologVC .view.frame=CGRect(x: ScreenWidth*6, y: 0, width: ScreenWidth, height: ScreenHeight-60)
    
    
    mainScrollView.addSubview(sportVC.view)
    mainScrollView.addSubview(headlinesVC.view)
    mainScrollView.addSubview(ecnomicsVC.view)
    mainScrollView.addSubview(recreationVC.view)
    mainScrollView.addSubview(militaryVC.view)
    mainScrollView.addSubview(historyVC.view)
    mainScrollView.addSubview(technologVC.view)

    }
    
}

extension NewsController:UIScrollViewDelegate{
     func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        let index = Int(offsetX/ScreenWidth)
        self.newsViewDelegate?.newsViewDidScroll(self, currentIndex: index,scrollOffsetX: offsetX)
    }
    
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        let index = Int(offsetX/ScreenWidth)
        let titleString = titleArray[index]
        self.name=titleString
//        if(titleString=="头条"){
//           self.headlinesVC.callBack("头条")
//             print("889------%@",titleString)
//        }
       
    }
    
}
extension NewsController:HeadTitleViewDelegate{
    func titleView(_ titleView: HeadTitleView, index: Int) {
        let indexF=CGFloat(index)
        mainScrollView.contentOffset=CGPoint(x: ScreenWidth*indexF, y: 0)
    }
}
